<template>
  <div>
    <user-form title="登录" @submit="onSubmit">
      <template #btn>
        <van-button block type="info" native-type="submit">登录</van-button>
      </template>
      <template #default>
        <router-link class="login-page" to="/register"
          >没有账号,去注册</router-link
        >
      </template>
    </user-form>
  </div>
</template>

<script>
//导入组件
import UserForm from "@/components/UserForm.vue";
import { loginAPI } from "@/api/user";
import { setToken } from "@/utils/storage";

export default {
  components: { UserForm },
  data() {
    return {
      username: "",
      password: "",
    };  
  },
  methods: {
    async onSubmit(values) {
      //调用函数
      const result = await loginAPI(values);
      setToken(result.data.token);
      //提示弹窗
      this.$toast.success("登录成功");
      //注册成功  跳转到登陆页面
      this.$router.push("/");
    },
  },
};
</script>

<style>
.login-page {
  float: right;
  margin-right: 9px;
  font-size: 12px;
}
</style>